﻿using C1.Phone.FlexGrid;
using C1.Silverlight.Resources;
using SmartMobile.Data;
using SMARTMobile.Models.Local;
using SMARTMobile.WinPhone;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SMARTMobile.Common;
using SMARTMobile.Services.SMARTMobileServiceReference;
using SMARTMobile.Models;
using SMARTMobile.Models.UI;

namespace SMARTMobile.ViewModels
{
    public partial class PhanTuyenViewModel : ViewModelBase
    {
        #region Fields
        private SmartMobileDBContext _smartMobileContext;
        private ObservableCollection<TCARCustModel> _tcARCustObservable;
        private ObservableCollection<TCARCustModel> _tcARCustNewObservable;
        private ObservableCollection<TCARCustModel> _tcARCustCopyObservable;
        private ObservableCollection<TCARCustModel> _tcARCustRemoveObservable;
        private List<TCARCustModel> _allTCARCustNew;
        private ObservableCollection<ARSALESP> _arSaleSPObservable;
        private ARSALESP _selectedARSaleSP;
        private ObservableCollection<string> _dayCollection;
        private ObservableCollection<string> _allWeekCollection;
        private ObservableCollection<string> _weekCollection; 
        private ObservableCollection<string> _fromWeekCollection;
        private ObservableCollection<string> _toWeekCollection;
        private ObservableCollection<TCARCUST> _allTCARCustObservable;
        private string _selectedDay;
        private string _selectedWeek;
        private string _selectedFromWeek;
        private string _selectedToWeek;  
        private ObservableCollection<ARCLASS> _arClassObservable;
        private ObservableCollection<ARSUBCLASS> _arSubClassObservable;
        private ObservableCollection<STRATEGICTYPE> _strategicTypeObservable;
        private ObservableCollection<ARSALESP> _nhanVienObservable;
        private ARSALESP _selectedNhanVien;
        private bool _isOpenChuyenTuyenWindow;
        private bool _isOpenChuaPTWindow;
        private bool _isOpenCopyWeekWindow;
        private bool _isOpenRemoveCustomerWindow;
        private bool _isGiamSat;
        
        #endregion
        
        public PhanTuyenViewModel()
        {
            this.CreateConnection();
            //_allTCARCustObservable = new ObservableCollection<TCARCUST>(this._smartMobileContext.TCARCUST);
            this.ArSaleSPObservable = new ObservableCollection<SmartMobile.Data.ARSALESP>();
            //this.GetArClass();
            //this.GetArSubClass();
            //this.GetStrategicType();

            #region Commands
            this.SaveCommand = new CommandRoute
            {
                ExecuteHandler = this.ExecuteSaveCommand
            };

            this.ExitCommand = new CommandRoute
            {
                ExecuteHandler = this.ExecuteExitCommand
            };

            this.ExitCopyCommand = new CommandRoute
            {
                ExecuteHandler = this.ExecuteExitCopyCommand
            };

            this.ExitKHChuaPTCommand = new CommandRoute
            {
                ExecuteHandler = this.ExecuteExitKHChuaPTCommand
            };

            this.ExitRemoveCommand = new CommandRoute
            {
                ExecuteHandler = this.ExecuteRemoveCommand
            };

            this.ExitCTCommand = new CommandRoute
            {
                ExecuteHandler = this.ExecuteExitCTCommand 
            };

            this.ChuyenTuyenCommand = new CommandRoute
            {
                ExecuteHandler = this.ExecuteChuyenTuyenCommand
            };

            this.KHChuaPhanTuyenCommand = new CommandRoute
            {
                ExecuteHandler = this.ExecuteKHChuaPhanTuyenCommand
            };

            this.ContinueCommand = new CommandRoute
            {
                ExecuteHandler = this.ExecuteContinueCommand
            };

            this.AcceptChuyenTuyenCommand = new CommandRoute
            {
                ExecuteHandler = this.ExecuteAcceptChuyenTuyenCommand 
            };

            this.CopyWeekCommand = new CommandRoute
            {
                ExecuteHandler = this.ExecuteCopyWeekCommand
            };

            this.RemoveCustomerCommand = new CommandRoute
            {
                ExecuteHandler = this.ExecuteRemoveCustomerCommand
            };

            this.ContinueCopyCommand = new CommandRoute
            {
                ExecuteHandler = this.ExecuteContinueCopyCommand
            };

            this.ContinueRemoveCommand = new CommandRoute
            {
                ExecuteHandler = this.ExecuteContinueRemoveCommand
            };

            this.GetDataCommand = new CommandRoute
            {
                ExecuteHandler = this.ExecuteGetDataCommand
            };

            #endregion
            ARSALESP arsale = new ARSALESP
            {
                ID_PK = Guid.NewGuid(),
                Salesp_name = "Chọn nhân viên",
                Salesp_key = "00",
                Sitecode = "00"
            };

            this.ArSaleSPObservable.Add(arsale);
            if (GlobalInformation.ArSaleSPType.Usertype == "DSR")
            {
                this.IsGiamSat = false;

                ARSALESP seletedSale = new ARSALESP
                {
                    ID_PK = Guid.NewGuid(),
                    Salesp_name = GlobalInformation.ArSaleSPType.Username,
                    Salesp_key = GlobalInformation.ArSaleSPType.Usercode,
                    Sitecode = GlobalInformation.ArSaleSPType.Sitecode
                };

                this.ArSaleSPObservable.Add(seletedSale);
                this.SelectedARSaleSP = seletedSale;
            }
            else
            {
                this.IsGiamSat = true;

                foreach (var item in this._smartMobileContext.ARSALESP)
                {
                    this.ArSaleSPObservable.Add(item);
                }
            }            

            if (this.ArSaleSPObservable.Any())
            {
                this.SelectedARSaleSP = this.ArSaleSPObservable[0];               
            }

            this.DayCollection = new ObservableCollection<string>
            {
                Localization.AppResources.ROUTECHECK_LABEL_ALL,
                Localization.AppResources.ROUTECHECK_LABEL_T2,
                Localization.AppResources.ROUTECHECK_LABEL_T3,
                Localization.AppResources.ROUTECHECK_LABEL_T4,
                Localization.AppResources.ROUTECHECK_LABEL_T5,
                Localization.AppResources.ROUTECHECK_LABEL_T6,
                Localization.AppResources.ROUTECHECK_LABEL_T7
            };

            this._allWeekCollection = new ObservableCollection<string>
            {
                Localization.AppResources.ROUTECHECK_LABEL_1,
                Localization.AppResources.ROUTECHECK_LABEL_2,
                Localization.AppResources.ROUTECHECK_LABEL_3,
                Localization.AppResources.ROUTECHECK_LABEL_4
            };

            this.WeekCollection = new ObservableCollection<string>(this._allWeekCollection);
            this.SelectedWeek = GlobalInformation.Week;
        }

        #region Properties

        public Dictionary<string, string> OutletTypeList  
        {
            get
            {
                var outletTypeList = this._smartMobileContext.OutletType.ToList();
                Dictionary<string, string> outletTypeDic = new Dictionary<string, string>();
                foreach (var item in outletTypeList)
                {
                    outletTypeDic.Add(item.OutletTypeCode, item.Description);
                }
                return outletTypeDic;
            }
        }

        public Dictionary<string, string> OutletGlobalTypeList 
        {
            get
            {
                var globalTypeList = this._smartMobileContext.OutletGlobalType.ToList(); 
                Dictionary<string, string> globalTypeDic = new Dictionary<string, string>(); 
                foreach (var item in globalTypeList)
                {
                    globalTypeDic.Add(item.GlobalTypeCode, item.Description);
                }
                return globalTypeDic;
            }
        }

        public Dictionary<string, string> GFKAreaList
        {
            get
            {
                var gfkAreaList = this._smartMobileContext.GFKArea.ToList();
                Dictionary<string, string> gfkAreaDic = new Dictionary<string, string>();
                foreach (var item in gfkAreaList)
                {
                    gfkAreaDic.Add(item.GFKAreaCode, item.Description);
                }
                return gfkAreaDic;
            }
        }

        public Dictionary<string, string> FreqList
        {
            get
            {
                var freqList = this._smartMobileContext.OutletVisitFrequency.ToList();
                Dictionary<string, string> freqDic = new Dictionary<string, string>();
                foreach (var item in freqList)
                {
                    freqDic.Add(item.FeqCode, item.Description);
                }
                return freqDic;
            }
        }

        public ObservableCollection<ARCLASS> ArClassObservable
        {
            get { return this._arClassObservable; }
            set
            {
                this._arClassObservable = value;
                base.RaisePropertyChanged("ArClassObservable");
            }
        }

        public ObservableCollection<ARSUBCLASS> ArSubClassObservable
        {
            get { return this._arSubClassObservable; }
            set
            {
                this._arSubClassObservable = value;
                base.RaisePropertyChanged("ArSubClassObservable");
            }
        }

        public ObservableCollection<STRATEGICTYPE> StrategicTypeObservable
        {
            get { return this._strategicTypeObservable; }
            set
            {
                this._strategicTypeObservable = value;
                base.RaisePropertyChanged("StrategicTypeObservable");
            }
        }

        public ObservableCollection<TCARCustModel> TCARCustObservable
        {
            get { return this._tcARCustObservable; }
            set
            {
                this._tcARCustObservable = value;
                base.RaisePropertyChanged("TCARCustObservable");
            }
        }

        public ObservableCollection<TCARCustModel> TCARCustNewObservable
        {
            get { return this._tcARCustNewObservable; }
            set
            {
                this._tcARCustNewObservable = value;
                base.RaisePropertyChanged("TCARCustNewObservable");
            }
        }

        public List<TCARCustModel> AllTCARCustNew
        {
            get { return this._allTCARCustNew; }
            set
            {
                this._allTCARCustNew = value;
                base.RaisePropertyChanged("AllTCARCustNew");
            }
        }

        public ObservableCollection<TCARCustModel> TCARCustRemoveObservable
        {
            get { return this._tcARCustRemoveObservable; }
            set
            {
                this._tcARCustRemoveObservable = value;
                base.RaisePropertyChanged("TCARCustRemoveObservable");
            }
        }

        public ObservableCollection<TCARCustModel> TCARCustCopyObservable
        {
            get { return this._tcARCustCopyObservable; }
            set
            {
                this._tcARCustCopyObservable = value;
                base.RaisePropertyChanged("TCARCustCopyObservable");
            }
        }

        public ObservableCollection<ARSALESP> ArSaleSPObservable
        {
            get { return this._arSaleSPObservable; }
            set
            {
                this._arSaleSPObservable = value;
                base.RaisePropertyChanged("ArSaleSPObservable");
            }
        }

        public ObservableCollection<ARSALESP> NhanVienObservable
        {
            get { return this._nhanVienObservable; }
            set
            {
                this._nhanVienObservable = value;
                base.RaisePropertyChanged("NhanVienObservable");
            }
        }

        public ARSALESP SelectedARSaleSP
        {
            get { return this._selectedARSaleSP; }
            set
            {
                this._selectedARSaleSP = value;
                base.RaisePropertyChanged("SelectedARSaleSP");
            }
        }

        public ObservableCollection<string> DayCollection
        {
            get { return this._dayCollection; }
            set
            {
                this._dayCollection = value;
                base.RaisePropertyChanged("DayCollection");
            }
        }

        public ObservableCollection<string> WeekCollection
        {
            get { return this._weekCollection; }
            set
            {
                this._weekCollection = value;
                base.RaisePropertyChanged("WeekCollection");
            }
        }

        public ObservableCollection<string> FromWeekCollection
        {
            get { return this._fromWeekCollection; }
            set
            {
                this._fromWeekCollection = value;
                base.RaisePropertyChanged("FromWeekCollection");
            }
        }

        public ObservableCollection<string> ToWeekCollection
        {
            get { return this._toWeekCollection; }
            set
            {
                this._toWeekCollection = value;
                base.RaisePropertyChanged("ToWeekCollection");
            }
        }

        public string SelectedDay
        {
            get { return this._selectedDay; }
            set
            {
                this._selectedDay = value;
                base.RaisePropertyChanged("SelectedDay");
            }
        }

        public string SelectedFromWeek
        {
            get { return this._selectedFromWeek; }
            set
            {
                this._selectedFromWeek = value;
                base.RaisePropertyChanged("SelectedFromWeek");
            }
        }

        public string SelectedToWeek
        {
            get { return this._selectedToWeek; }
            set
            {
                this._selectedToWeek = value;
                base.RaisePropertyChanged("SelectedToWeek");
            }
        }

        public string SelectedWeek
        {
            get { return this._selectedWeek; }
            set
            {
                this._selectedWeek = value;
                base.RaisePropertyChanged("SelectedWeek");
            }
        }

        public bool IsOpenChuyenTuyenWindow 
        {
            get { return this._isOpenChuyenTuyenWindow; }
            set
            {
                this._isOpenChuyenTuyenWindow = value;
                base.RaisePropertyChanged("IsOpenChuyenTuyenWindow");
            }
        }

        public bool IsOpenChuaPTWindow 
        {
            get { return this._isOpenChuaPTWindow; }
            set
            {
                this._isOpenChuaPTWindow = value;
                base.RaisePropertyChanged("IsOpenChuaPTWindow");
            }
        }

        public bool IsOpenCopyWeekWindow
        {
            get { return this._isOpenCopyWeekWindow; }
            set
            {
                this._isOpenCopyWeekWindow = value;
                base.RaisePropertyChanged("IsOpenCopyWeekWindow");
            }
        }

        public bool IsOpenRemoveCustomerWindow
        {
            get { return this._isOpenRemoveCustomerWindow; }
            set
            {
                this._isOpenRemoveCustomerWindow = value;
                base.RaisePropertyChanged("IsOpenRemoveCustomerWindow");
            }
        }

        public bool IsGiamSat
        {
            get { return this._isGiamSat; }
            set
            {
                this._isGiamSat = value;
                base.RaisePropertyChanged("IsGiamSat"); 
            }
        }

        public ARSALESP SelectedNhanVien
        {
            get { return this._selectedNhanVien; }
            set
            {
                this._selectedNhanVien = value;
                base.RaisePropertyChanged("SelectedNhanVien");
            }
        }

        #endregion

        #region Command

        public ICommandRoute ExitCommand
        {
            get;

            private set;
        }

        public ICommandRoute ExitCopyCommand
        {
            get;

            private set;
        }

        public ICommandRoute ExitKHChuaPTCommand
        {
            get;

            private set;
        }

        public ICommandRoute ExitRemoveCommand
        {
            get;

            private set;
        }

        public ICommandRoute ExitCTCommand
        {
            get;

            private set;
        }

        public ICommandRoute SaveCommand
        {
            get;

            private set;
        }

        public ICommandRoute ContinueCommand
        {
            get;

            private set;
        }

        public ICommandRoute AcceptChuyenTuyenCommand 
        {
            get;

            private set;
        }

        public ICommandRoute KHChuaPhanTuyenCommand
        {
            get;

            private set;
        }

        public ICommandRoute ChuyenTuyenCommand 
        {
            get;

            private set;
        }

        public ICommandRoute CopyWeekCommand
        {
            get;

            private set;
        }

        public ICommandRoute RemoveCustomerCommand 
        {
            get;

            private set;
        }

        public ICommandRoute ContinueCopyCommand
        {
            get;

            private set;
        }

        public ICommandRoute ContinueRemoveCommand
        {
            get;

            private set;
        }

        public ICommandRoute GetDataCommand 
        {
            get;

            private set;
        }

        #endregion

        #region Command Action Handlers

        private void ExecuteExitCommand(object commandParams)
        {
            this.ExitCommand.RaiseCanExecuteChanged(true);
            base.PageNavigation.Navigate("Main");
        }
         
        private void ExecuteSaveCommand(object commandParams)
        {            
            List<TCARCustModel> tcArCust = this.TCARCustObservable.Where(t => t.IsSelected == true).ToList();
            if (tcArCust.Count == 0)
            {
                this.ShowMessage("Vui lòng chọn khách hàng");
                this.SaveCommand.RaiseCanExecuteChanged(true);
                return;
            }

            this.IsBusy = true;
            foreach (var item in tcArCust)
            {
                TCARCUST tcArCustDB = this._smartMobileContext.TCARCUST.Where(t => t.Customerkey == item.Customerkey).FirstOrDefault();
                if (tcArCustDB != null)
                {
                    tcArCustDB.Salespkey = this.SelectedARSaleSP.Salesp_key;
                    tcArCustDB.T2 = (item.T2 == true ? 1 : 0).ToString();
                    tcArCustDB.T3 = (item.T3 == true ? 1 : 0).ToString();
                    tcArCustDB.T4 = (item.T4 == true ? 1 : 0).ToString();
                    tcArCustDB.T5 = (item.T5 == true ? 1 : 0).ToString();
                    tcArCustDB.T6 = (item.T6 == true ? 1 : 0).ToString();
                    tcArCustDB.T7 = (item.T7 == true ? 1 : 0).ToString();

                    tcArCustDB.GFKArea = item.GFKAreaCode;
                    tcArCustDB.OutletTypeCode = item.OutletTypeCode;
                    tcArCustDB.GlobalTypeCode = item.GlobalTypeCode;
                    tcArCustDB.Freq = item.Freq;
                }

                this._smartMobileContext.SubmitChanges();
            }
            this.IsBusy = false;

            this.ShowMessage("Lưu thành công");
            this.SaveCommand.RaiseCanExecuteChanged(true);
        }

        // Chuyen tuyen
        private void ExecuteChuyenTuyenCommand(object commandParams)
        {
            this.NhanVienObservable = new ObservableCollection<ARSALESP>(this.ArSaleSPObservable);
            this.NhanVienObservable.Remove(this.SelectedARSaleSP);
            this.NhanVienObservable.RemoveAt(0);

            if (this.NhanVienObservable.Count == 0)
            {
                this.ShowMessage("Vui lòng chọn nhân viên");
                this.IsOpenChuyenTuyenWindow = false;
                this.ChuyenTuyenCommand.RaiseCanExecuteChanged(true);
                return;
            }

            this.IsOpenChuyenTuyenWindow = true;            
            this.ChuyenTuyenCommand.RaiseCanExecuteChanged(true);
        }

        private void ExecuteAcceptChuyenTuyenCommand(object commandParams)
        {
            List<TCARCustModel> tcArCust = new List<TCARCustModel>(this.TCARCustObservable.Where(t => t.IsSelected == true));

            if (tcArCust.Count == 0)
            {
                this.ShowMessage("Vui lòng chọn khách hàng");                
                this.AcceptChuyenTuyenCommand.RaiseCanExecuteChanged(true);
                return;
            }

            this.IsBusy = true;
            
            foreach (var item in tcArCust)
            {
                TCARCUST tcArCustDB = this._smartMobileContext.TCARCUST.Where(t => t.Customerkey == item.Customerkey).FirstOrDefault();
                if (tcArCustDB != null)
                {
                    tcArCustDB.Salespkey = this.SelectedNhanVien.Salesp_key;
                    tcArCustDB.T2 = (item.T2 == true ? 1 : 0).ToString();
                    tcArCustDB.T3 = (item.T3 == true ? 1 : 0).ToString();
                    tcArCustDB.T4 = (item.T4 == true ? 1 : 0).ToString();
                    tcArCustDB.T5 = (item.T5 == true ? 1 : 0).ToString();
                    tcArCustDB.T6 = (item.T6 == true ? 1 : 0).ToString();
                    tcArCustDB.T7 = (item.T7 == true ? 1 : 0).ToString();

                    tcArCustDB.GFKArea = item.GFKAreaCode;
                    tcArCustDB.OutletTypeCode = item.OutletTypeCode;
                    tcArCustDB.GlobalTypeCode = item.GlobalTypeCode;
                    //tcArCustDB.Freq = string.IsNullOrEmpty(item.Freq) ?? 0 : Convert.ToInt32(item.Freq);
                }

                this._smartMobileContext.SubmitChanges();
                this.TCARCustObservable.Remove(item);
            }

            this.IsBusy = false;
            this.AcceptChuyenTuyenCommand.RaiseCanExecuteChanged(true);
            this.ShowMessage("Lưu thành công");
        }

        private void ExecuteExitCTCommand(object commandParams) 
        {
            this.IsOpenChuyenTuyenWindow = false;
            this.ExitCTCommand.RaiseCanExecuteChanged(true);
        }

        // Chua phan tuyen
        private void ExecuteKHChuaPhanTuyenCommand(object commandParams)
        {
            List<ARCUST> tcArCust = this._smartMobileContext.ARCUST.ToList();
            if (tcArCust.Count == 0)
            {
                this.ShowMessage("Không có khách hàng chưa phân tuyến");
                this.KHChuaPhanTuyenCommand.RaiseCanExecuteChanged(true);
                return;
            }

            this.IsOpenChuaPTWindow = true;

            this.IsBusy = true;
            this.TCARCustNewObservable = new ObservableCollection<TCARCustModel>();
            this._allTCARCustNew = new List<TCARCustModel>();
            foreach (var item in tcArCust)
            {
                TCARCustModel model = new TCARCustModel
                {
                    Customerkey = item.Customerkey,
                    CustomerName = item.Customername,
                    IsSelected = false,
                    Address = item.Address                 
                };

                this.TCARCustNewObservable.Add(model);
                this._allTCARCustNew.Add(model);
            }
            this.KHChuaPhanTuyenCommand.RaiseCanExecuteChanged(true);
            this.IsBusy = false;            
        }

        private void ExecuteExitKHChuaPTCommand(object commandParams)
        {
            this.IsOpenChuaPTWindow = false;
            this.ExitKHChuaPTCommand.RaiseCanExecuteChanged(true);
        }

        private void ExecuteContinueCommand(object commandParams)   
        {            
            List<TCARCustModel> tcArCust = this.TCARCustNewObservable.Where(t => t.IsSelected == true).ToList();
            if (tcArCust.Count == 0)
            {
                this.ContinueCommand.RaiseCanExecuteChanged(true);
                this.ShowMessage("Vui lòng chọn khách hàng");
                return;
            }

            this.IsBusy = true;

            foreach (var item in tcArCust)
            {
                TCARCustModel model = new TCARCustModel
                {
                    Customerkey = item.Customerkey,
                    CustomerName = item.CustomerName,
                    IsSelected = false,
                    Address = item.Address,
                    T2 = false,
                    T3 = false,
                    T4 = false,
                    T5 = false,
                    T6 = false,
                    T7 = false,
                    Freq = string.Empty,
                    GlobalTypeCode = string.Empty,
                    OutletTypeCode = string.Empty,
                    GFKAreaCode = string.Empty,
                };
                                
                this.TCARCustObservable.Add(item);

                this._smartMobileContext.ARCUST.DeleteOnSubmit(this._smartMobileContext.ARCUST.Where(x => x.Customerkey == item.Customerkey).FirstOrDefault());
                this._smartMobileContext.SubmitChanges();
            }

            this.ContinueCommand.RaiseCanExecuteChanged(true);
            this.IsBusy = false;

            this.ShowMessage("Lưu thành công");
        }        

        // Copy week
        private void ExecuteCopyWeekCommand(object commandParams) 
        {
            this.IsOpenCopyWeekWindow = true;
            this.FromWeekCollection = new ObservableCollection<string>(this._allWeekCollection);
            this.ToWeekCollection = new ObservableCollection<string>(this._allWeekCollection);

            this.SelectedFromWeek = this._allWeekCollection[0];
            this.SelectedToWeek = this._allWeekCollection[1];
            this.CopyWeekCommand.RaiseCanExecuteChanged(true);
        }

        private void ExecuteGetDataCommand(object commandParams) 
        {
            List<TCARCUST> tcArCustList = this._smartMobileContext.TCARCUST.Where(t => t.Week == SelectedFromWeek).ToList();
            if (tcArCustList.Count == 0)
            {
                this.ShowMessage("Không có dữ liệu của tuần " + this.SelectedFromWeek);
                this.GetDataCommand.RaiseCanExecuteChanged(true);
                return;
            }

            this.IsBusy = false;

            this.TCARCustCopyObservable = new ObservableCollection<TCARCustModel>();
            TCARCustModel tcArCustModel;
            foreach (var item in tcArCustList)
            {
                tcArCustModel = new TCARCustModel
                {
                    IsSelected = true,
                    Customerkey = item.Customerkey,
                    CustomerName = item.Customername
                };
                this.TCARCustCopyObservable.Add(tcArCustModel);
            }

            this.IsBusy = false;
            this.GetDataCommand.RaiseCanExecuteChanged(true);
        }

        private void ExecuteContinueCopyCommand(object commandParams) 
        {
            if (this.TCARCustCopyObservable == null)
            {
                this.ShowMessage("Không có dữ liệu của tuần " + this.SelectedFromWeek);
                this.ContinueCopyCommand.RaiseCanExecuteChanged(true);
                return;
            }

            if (this.SelectedFromWeek == this.SelectedToWeek)
            {
                this.ShowMessage("Tuần giống nhau");
                this.ContinueCopyCommand.RaiseCanExecuteChanged(true);
                return;
            }

            List<TCARCustModel> tcArCust = this.TCARCustCopyObservable.Where(t => t.IsSelected == true).ToList();
            if (tcArCust.Count == 0)
            {
                this.ShowMessage("Vui lòng chọn khách hàng cần chuyển dữ liệu");
                this.ContinueCopyCommand.RaiseCanExecuteChanged(true);
                return;
            }

            this.IsBusy = true;            
            foreach (var item in tcArCust)
            {
                TCARCUST tcArCustDB = this._smartMobileContext.TCARCUST.Where(t => t.Customerkey == item.Customerkey).FirstOrDefault();
                TCARCUST newTCARCUST = new TCARCUST();
                tcArCustDB.CopyTo(newTCARCUST);
                newTCARCUST.ID_PK = Guid.NewGuid();
                newTCARCUST.Week = this.SelectedToWeek;

                this._smartMobileContext.TCARCUST.InsertOnSubmit(newTCARCUST);
                this._smartMobileContext.SubmitChanges();             
            }

            this.IsBusy = false;
            this.ContinueCopyCommand.RaiseCanExecuteChanged(true);
            this.ShowMessage("Lưu thành công");
        }

        private void ExecuteExitCopyCommand(object commandParams)
        {
            this.IsOpenCopyWeekWindow = false;
            this.ExitCopyCommand.RaiseCanExecuteChanged(true);
        }

        // Remove
        private void ExecuteRemoveCustomerCommand(object commandParams) 
        {
            this.TCARCustRemoveObservable = new ObservableCollection<TCARCustModel>(this.TCARCustObservable.Where(t => t.IsSelected == true));
            if (TCARCustRemoveObservable.Count == 0)
            {
                this.ShowMessage("Vui lòng chọn khách hàng");
                this.RemoveCustomerCommand.RaiseCanExecuteChanged(true);
                return;
            }

            this.IsOpenRemoveCustomerWindow = true;
            this.RemoveCustomerCommand.RaiseCanExecuteChanged(true);
        }

        private void ExecuteContinueRemoveCommand(object commandParams) 
        {
            List<TCARCustModel> tcArCust = this.TCARCustRemoveObservable.Where(t => t.IsSelected == true).ToList();
            if (tcArCust.Count == 0)
            {
                this.ShowMessage("Vui lòng chọn khách hàng");
                this.RemoveCustomerCommand.RaiseCanExecuteChanged(true);
                return;
            }

            this.IsBusy = true;           
            foreach (var item in tcArCust)
            {
                TCARCUST tcArCustDB = this._smartMobileContext.TCARCUST.Where(t => t.Customerkey == item.Customerkey).FirstOrDefault();
                this._smartMobileContext.TCARCUST.DeleteOnSubmit(tcArCustDB);
                this._smartMobileContext.SubmitChanges();
                this.TCARCustObservable.Remove(item);
            }

            this.IsBusy = false;
            this.ContinueRemoveCommand.RaiseCanExecuteChanged(true);


            this.ShowMessage("Lưu thành công");
        }

        private void ExecuteRemoveCommand(object commandParams)
        {
            this.IsOpenRemoveCustomerWindow = false;
            this.ExitRemoveCommand.RaiseCanExecuteChanged(true);
        }
        #endregion

        #region Method
        private void GetArClass()
        {
            this.ArClassObservable = new ObservableCollection<ARCLASS>(this._smartMobileContext.ARCLASS);
        }

        private void GetArSubClass()
        {
            this.ArSubClassObservable = new ObservableCollection<ARSUBCLASS>(this._smartMobileContext.ARSUBCLASS);
        }
        private void GetStrategicType()
        {
            this.StrategicTypeObservable = new ObservableCollection<STRATEGICTYPE>(this._smartMobileContext.STRATEGICTYPE);
        }

        private void CreateConnection()
        {
            this._smartMobileContext = new SmartMobileDBContext(SmartMobileDBContext.ConnectionString);
            this._smartMobileContext.CreateIfNotExists();
            this._smartMobileContext.LogDebug = true;
        }
        #endregion
        
    }
}
